﻿using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Utilities;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using System;

namespace KZ.Lottery.DAL
{
    public class ReportDAL
    {
        public static decimal GetReportForRevenue(DateTime? date)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("Date", SqlDbType.Date).Value = date;
                DataAccessHelper.SqlCmd.Parameters.Add("Revenue", SqlDbType.NVarChar, 50);

                DataAccessHelper.SqlCmd.Parameters["Revenue"].Direction = ParameterDirection.Output;

                DataAccessHelper.ExecuteNonQueryNotClearCommand(CommandType.StoredProcedure, KZ_StoreName.ReportGetForRevenue);

                decimal revenue = Utility.DecimalParse(DataAccessHelper.SqlCmd.Parameters["Revenue"].Value);

                DataAccessHelper.ClearCommand();

                return revenue;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetReportForRevenue", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_REPORT_GET);
            }
        }

        public static DataTable GetReportForChannel(string ruleID, DateTime? from, DateTime? to)
        {
            try
            {
                DataTable dt = new DataTable();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;

                dt.Load(DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.ReportGetForChannel));

                return dt;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetReportForChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_REPORT_GET);
            }
        }

        public static DataTable GetReportForRuleChannel(string ruleID, DateTime? from, DateTime? to)
        {
            try
            {
                DataTable dt = new DataTable();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;

                dt.Load(DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.ReportGetForRuleChannel));

                return dt;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetReportForRuleChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_REPORT_GET);
            }
        }

        public static DataTable GetReportForRuleChannelTotal(string ruleID, DateTime? from, DateTime? to)
        {
            try
            {
                DataTable dt = new DataTable();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;

                dt.Load(DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.ReportGetForChannelTotal));

                return dt;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetReportForRuleChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_REPORT_GET);
            }
        }

        public static DataTable GetReportForDate(string ruleID, DateTime? from, DateTime? to)
        {
            try
            {
                DataTable dt = new DataTable();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;

                dt.Load(DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.ReportGetForDate));

                return dt;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetReportForDate", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_REPORT_GET);
            }
        }

        public bool InsertReport(Entities.Report report)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("Date", SqlDbType.Date).Value = report.Date;
                DataAccessHelper.SqlCmd.Parameters.Add("RuleDetailID", SqlDbType.VarChar).Value = report.RuleDetailID;
                DataAccessHelper.SqlCmd.Parameters.Add("ChannelID", SqlDbType.VarChar).Value = report.ChannelID;
                DataAccessHelper.SqlCmd.Parameters.Add("Head", SqlDbType.VarChar).Value = report.Head;
                DataAccessHelper.SqlCmd.Parameters.Add("Tail", SqlDbType.VarChar).Value = report.Tail;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadCapital", SqlDbType.Decimal).Value = report.HeadCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("TailCapital", SqlDbType.Decimal).Value = report.TailCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadActualCapital", SqlDbType.Decimal).Value = report.HeadActualCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("TailActualCapital", SqlDbType.Decimal).Value = report.TailActualCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadWinAmount", SqlDbType.Decimal).Value = report.HeadWinAmount;
                DataAccessHelper.SqlCmd.Parameters.Add("TailWinAmount", SqlDbType.Decimal).Value = report.TailWinAmount;

                DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.ReportInsert);
                return true;
            }
            catch (SqlException ex)
            {
                Log.LogEx("InsertReport", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAILGROUPREPORT_INSERT);
            }
        }
    }
}
